﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using MySql.Data.MySqlClient;

namespace AdministrationWeb.Administration
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private const string CONNECTION_STRING_NAME = "ApplicationConnectionString";

        protected void Page_Load(object sender, EventArgs e)
        {
            txtStudentTemp.Focus();
            if (!IsPostBack)
            {
                string getTemplates = "select * from email_template";

                MySqlConnection conn = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString);
                MySqlCommand command = new MySqlCommand(getTemplates, conn);

                try
                {
                    conn.Open();

                    MySqlDataReader reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        string templateType = reader.GetString("template_type");
                        string templateText = reader.GetString("email_text");

                        switch (templateType)
                        {
                            case "ADMIN":
                                txtMaintenanceTemp.Text = templateText;
                                break;
                            case "STUDENT":
                                txtStudentTemp.Text = templateText;
                                break;
                            default:
                                break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    lblMessage.Visible = true;
                    lblMessage.Text = ex.Message;
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string updateStudentTemplate = "update email_template set email_text = '" + txtStudentTemp.Text +
                                            "' where template_type = 'STUDENT'";
            string updateAdminTemplate = "update email_template set email_text = '" + txtMaintenanceTemp.Text +
                                            "' where template_type = 'ADMIN'";
            MySqlConnection conn = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString);
            MySqlCommand updateStudentCmd = new MySqlCommand(updateStudentTemplate, conn);
            MySqlCommand updateAdminCmd = new MySqlCommand(updateAdminTemplate, conn);

            try
            {
                conn.Open();

                //updateAdminCmd.ExecuteNonQuery();
                updateStudentCmd.ExecuteNonQuery();
                Server.Transfer("UpdateSuccess.aspx");
            }
            catch (Exception ex)
            {
                lblMessage.Visible = true;
                lblMessage.Text = ex.Message;
            }
            finally
            {
                conn.Close();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Server.Transfer("default.aspx");
        }
    }
}